package com.pt25.base.dep.db;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.pt25.base.web.WebThreadLocal;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.StringValue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * 添加分页插件
 */
@Slf4j
@Configuration
public class MyBatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        //集成多租户隔离功能
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() {
            @Override
            public Expression getTenantId() {
                return new StringValue(WebThreadLocal.get().getTenantId());
            }

            @Override
            public String getTenantIdColumn() {
                return "tenant_id";
            }

            @Override
            public boolean ignoreTable(String tableName) {
                log.info("传入的表名={}", tableName);
                //String tableNames = "aim_config_log,aim_config_value";
                // String tableNames = "aim_aibrain_case,aim_aibrain_intention_data,aim_aibrain_intention_process,aim_config_data,aim_config_type,aim_cvid_accumulate_data,aim_sf_arrange,aim_sf_conf_task,aim_sf_manual_conf,aim_sf_schedule,aim_sf_schedule_history,aim_sf_traffic_conf,aim_sf_user_arrange,aim_xfj_album_data,aim_xfj_dynamics_data,aim_xfj_family_data,aim_xfj_family_user_data,aim_xfj_media_data,aim_xfj_user_data,cis_app_intention_and_slot_desc,cis_badcase_sample,cis_badcase_test_result,cis_fixed_intention_sample,cis_intention_test_result,cis_llm_generate_sample,cis_model_training_progress,cis_multi_intention_sample,cis_multi_turn_intention_sample,cis_similar_intention_sample,cis_user_basic_sample,data_for_voice_data,film_tag_data,intention_show_data,iptv_epg_video_info,model,model_sample,model_train,test,user_policy_mapping_content_data_d";
                //Set<String> tableNameSet = Arrays.stream(tableNames.split(",")).map(String::toLowerCase).collect(Collectors.toSet());
                String lowerCaseTableName = tableName.toLowerCase();
                return lowerCaseTableName.startsWith("sys_admin_") || lowerCaseTableName.startsWith("sys_config_") || lowerCaseTableName.contains("sharding");
            }
        }));
        //添加分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}
